﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient


Public Class ProvinciasDB
    Public Shared Function GetProvinciasDePais(ByVal id_Pais As Integer) As DataSet
        Dim cmd As New SqlCommand("spGetProvincias", DB.Conexion)
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        cmd.CommandType = CommandType.StoredProcedure
        If id_Pais <> 0 Then
            cmd.Parameters.AddWithValue("@id_pais", id_Pais)
        End If

        da.SelectCommand = cmd
        da.Fill(ds, "Provincias")
        Return ds
    End Function
    Public Shared Sub InsertarProvincia(ByVal Provincia As String, ByVal id_pais As Integer)
        Dim cmd As New SqlCommand("spInsertProvincia", DB.Conexion)

        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_pais", id_pais)
        cmd.Parameters.AddWithValue("@provincia", Provincia)
        cmd.Parameters.Add("@error", SqlDbType.Int)
        cmd.Parameters.Add("@errorDesc", SqlDbType.VarChar, 200)
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

        If cmd.Parameters("@error").Value <> 0 Then
            Throw New ApplicationException(cmd.Parameters("@errorDesc").Value.ToString())
        End If

    End Sub

    Public Shared Sub EliminarProvincia(ByVal id_provincia As Integer)
        Dim cmd As New SqlCommand("spDeleteProvincia", DB.Conexion)

        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.AddWithValue("@id_provincia", id_provincia)
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub

    Public Shared Sub ActualizarProvincia(ByVal id_provincia As Integer, ByVal provincia As String)
        Dim cmd As New SqlCommand("spUpdateProvincia", DB.Conexion)

        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_provincia", id_provincia)
        cmd.Parameters.AddWithValue("@provincia", provincia)
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub
End Class
